package com.nike.amoeba.viewmodel

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.nike.amoeba.bean.AppRoomDataBase
import com.nike.amoeba.bean.db.TaskAssetBean
import com.nike.amoeba.bean.db.TaskStockBean
import com.nike.amoeba.util.CacheUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.callback.databind.StringObservableField

/**
 * @Author nike
 * @Date 2023/7/7 12:00
 * @Description
 */
class StockOrderModel: BaseViewModel() {

    var triggerLocation = StringObservableField()

    var username = StringObservableField()

    var time = StringObservableField()

    var listBean: MutableLiveData<ArrayList<TaskAssetBean>> = MutableLiveData()

    var listTaskStockBean: MutableLiveData<ArrayList<TaskStockBean>> = MutableLiveData()

    val RoNo = CacheUtil.getUser()?.RoNo

    val companyId = CacheUtil.getUser()?.companyId

    val taskAssetDao = AppRoomDataBase.get().getTaskAssetDao()

    val assetDao = AppRoomDataBase.get().getAssetDao()

    val taskStockDao = AppRoomDataBase.get().getTaskStockDao()

    val existEpcDao = AppRoomDataBase.get().getExistEpcDao()

    val stockEpcDao = AppRoomDataBase.get().getStockEpcDao()

    fun onRequest() {
        viewModelScope.launch(Dispatchers.IO) {
            if (RoNo != null) {
                val list = taskStockDao.findById(RoNo, companyId, null)
                list.forEach {
                    var list = stockEpcDao.findOrderIdAll(it.OrderNo!!)
                    val split = it.Inventory_progress?.split("/")
                    it.Inventory_progress = "${list.size}" + "/" + (split?.get(1) ?: "")
                }
                listTaskStockBean.postValue(list as ArrayList<TaskStockBean>)
            }
        }
    }

}